//
//  LZComDoctorVC.m
//  DoctorFAQs
//
//  Created by langZai on 2017/3/30.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZComDoctorVC.h"
#import "LZComDoctorView.h"
#import "LZComDoctorModel.h"

@interface LZComDoctorVC ()<UITextViewDelegate>

/** 医生那一行的view */
@property (nonatomic, strong) LZComDoctorView *doctorView;
/** textView */
@property (nonatomic, strong) UITextView *ComTextView;
/** 占位Label */
@property (nonatomic, strong) UILabel *placeHolderLabel;
/** 投诉按钮 */
@property (nonatomic, strong) UIButton *comButton;
/** 模型 */
@property (nonatomic, strong) LZComDoctorModel *lz_comDoctorModel;
@end

@implementation LZComDoctorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatUI];
//    [self getData];
    self.view.backgroundColor = KHColorFromRGB(0xf8f8f8);
    
}
- (void)setOrder_num:(NSString *)order_num {
    _order_num = order_num;
    [self getData];
}
- (void)creatUI {
    _doctorView = [[LZComDoctorView alloc] initWithFrame:CGRectMake(0, 10*ysIZE, SCREEN_Width, 90*ysIZE)];
    // 传模型
    
    [self.view addSubview:_doctorView];
    
    // 投诉类容
    UILabel *comLabel = [[UILabel alloc] init];
    comLabel.text = @"投诉内容";
    comLabel.font = NEWFONT(18);
    [self.view addSubview:comLabel];
    [comLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10*xsIZE);
        make.top.equalTo(_doctorView.mas_bottom).offset(13*ysIZE);
    }];
    
    // 投诉输入文本
    _ComTextView = [[UITextView alloc] init];
    _ComTextView.tintColor = KHGreenColor;
    _ComTextView.delegate = self;
    [self.view addSubview:_ComTextView];
    [_ComTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.offset(200*ysIZE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(_doctorView.mas_bottom).offset(40*ysIZE);
        
    }];
    
    // 占位label
    _placeHolderLabel = [[UILabel alloc] init];
    _placeHolderLabel.text = @"填写投诉内容...";
    _placeHolderLabel.textColor = KHColorFromRGB(0x999999);
    _placeHolderLabel.font = NEWFONT(18);
    [_ComTextView addSubview:_placeHolderLabel];
    [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(_ComTextView).offset(10*xsIZE);
    }];
    
    // 投诉按钮
    _comButton = [[UIButton alloc] init];
    [_comButton setTitle:@"我要投诉" forState:UIControlStateNormal];
    [_comButton setBackgroundColor:KHColorFromRGB(0x4bb92c)];
    _comButton.titleLabel.font = NEWFONT(18);
    [_comButton setTitleColor:KHColorFromRGB(0xffffff) forState:UIControlStateNormal];
    _comButton.layer.cornerRadius = 5;
    _comButton.layer.masksToBounds = YES;
    [self.view addSubview:_comButton];
    [_comButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(280*xsIZE);
        make.height.offset(45*ysIZE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(_ComTextView.mas_bottom).offset(25*ysIZE);
    }];
    
    [_comButton addTarget:self action:@selector(comButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)getData {
    ESWeak(self, weakSelf);
    NSDictionary *paramas = @{@"order_num":self.order_num};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Order/complainView" sucessBlock:^(id content) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"---投诉数据--%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            [weakSelf dealDataWithDict:dict];
        }else {
            [YJProgressHUD showMessage:@"投诉失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"投诉失败" inView:self.view];
    }];
}

// 处理拿到的数据
- (void)dealDataWithDict:(NSDictionary *)dict {
    
    NSDictionary *tempDict = dict[@"list"];
    _lz_comDoctorModel = [[LZComDoctorModel alloc] initWithDict:[tempDict mutableCopy]];
    _doctorView.lz_doctorModel = _lz_comDoctorModel;
}


// 点击投诉按钮
- (void)comButtonClick {
    
    if ([_ComTextView.text isEqualToString:@""]) {
        [YJProgressHUD showMessage:@"还没有投诉内容哦~" inView:self.view];
        return;
    }
    
    NSString * comText = [_ComTextView.text stringByReplacingOccurrencesOfString:@"\"" withString:@"”" ];
    
    NSDictionary *paramas = @{@"did":_lz_comDoctorModel.did,@"content":comText};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Order/complain" sucessBlock:^(id content) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"投诉成功！" inView:self.view];
            _ComTextView.text = @"";
            [self.navigationController popToRootViewControllerAnimated:YES];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [YJProgressHUD showMessage:@"投诉失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"投诉失败" inView:self.view];
    }];
}

- (void)textViewDidChange:(UITextView *)textView {
    //    对占位符的显示和隐藏做判断
    if (self.ComTextView.text.length == 0) {
        self.placeHolderLabel.text =  @"填写投诉内容...";
    }else {
        self.placeHolderLabel.text = @"";
    }
}



@end
